package com.school.controller.student;

import com.school.model.Student;
import com.school.service.StudentService;
import com.school.service.impl.StudentServiceImpl;
import com.school.util.ResponseJsonUtil;
import com.school.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student/page")
public class FindPageController extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 插入 数据
        Student student = new Student();
        student.setStunum(req.getParameter("stunum"));
        student.setName(req.getParameter("name"));
        student.setSex(req.getParameter("sex"));
        String page = req.getParameter("page");
        String size = req.getParameter("size");

        Integer p = 1;
        if (page != null) {
            p = Integer.valueOf(page);
        }
        Integer s = 10;
        if (page != null) {
            s = Integer.valueOf(size);
        }
        // 调用Service中的方法
        List<Student> res = studentService.findPage(student, p, s);
        if (res != null && res.size() != 0) {
            // json格式传递到前端
            ResponseJsonUtil.write(resp, R.ok(res));
        } else {
            // json格式传递到前端
            ResponseJsonUtil.write(resp, R.err());
        }
    }
}
